package com.tc.controller;


import com.tc.domain.Medical_services;
import com.tc.dto.Medical_servicesDTO;
import com.tc.dto.Medical_servicesQueryDTO;
import com.tc.service.IMedical_servicesService;
import com.tc.utils.ResultVo;
import com.tc.vo.Medical_servicesVO;
import com.tc.vo.PageResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author tc
 * @since 2025-07-09
 */
@RestController
@Api(tags = "诊疗项目管理")
@RequestMapping("/medical_services")
public class Medical_servicesController {

    @Autowired
    private IMedical_servicesService medical_servicesService;

    @ApiOperation("分页查询诊疗项目信息")
    @PostMapping("/page")
    public ResultVo<PageResultVO<Medical_servicesVO>> page(@RequestBody Medical_servicesQueryDTO queryDTO) {
        try {
            // 调用Service层方法进行复杂查询
            PageResultVO<Medical_servicesVO> pageVO = medical_servicesService.pageQuery(queryDTO);
            return ResultVo.ok(pageVO);
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("根据ID查询诊疗项目信息")
    @GetMapping("/{id}")
    public ResultVo<Medical_servicesVO> getById(@PathVariable("id") Long id) {
        try {
            // 简单查询直接使用MyBatis-Plus
            Medical_services medical_services = medical_servicesService.getById(id);
            if (medical_services != null) {
                Medical_servicesVO vo = new Medical_servicesVO();
                BeanUtils.copyProperties(medical_services, vo);
                return ResultVo.ok(vo);
            } else {
                return ResultVo.fail("诊疗项目信息不存在");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("新增诊疗项目信息")
    @PostMapping("/add")
    public ResultVo<Boolean> add(@RequestBody Medical_servicesDTO medical_servicesDTO) {
        try {
            // 简单的数据保存操作，直接使用MyBatis-Plus
            Medical_services medical_services = new Medical_services();
            BeanUtils.copyProperties(medical_servicesDTO, medical_services);
            
            boolean result = medical_servicesService.save(medical_services);
            if (result) {
                return ResultVo.ok(true, "新增诊疗项目信息成功");
            } else {
                return ResultVo.fail("新增诊疗项目信息失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("修改诊疗项目信息")
    @PutMapping("/update")
    public ResultVo<Boolean> update(@RequestBody Medical_servicesDTO medical_servicesDTO) {
        try {
            // 判断ID是否存在
            if (medical_servicesDTO.getServiceId() == null) {
                return ResultVo.fail("诊疗项目ID不能为空");
            }
            
            // 判断诊疗项目是否存在
            Medical_services existMedical_services = medical_servicesService.getById(medical_servicesDTO.getServiceId());
            if (existMedical_services == null) {
                return ResultVo.fail("诊疗项目信息不存在");
            }
            
            // 简单的更新操作，直接使用MyBatis-Plus
            Medical_services medical_services = new Medical_services();
            BeanUtils.copyProperties(medical_servicesDTO, medical_services);
            
            boolean result = medical_servicesService.updateById(medical_services);
            if (result) {
                return ResultVo.ok(true, "修改诊疗项目信息成功");
            } else {
                return ResultVo.fail("修改诊疗项目信息失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }

    @ApiOperation("删除诊疗项目信息")
    @PostMapping("/delete/{id}")
    public ResultVo<Boolean> delete(@PathVariable("id") Long id) {
        try {
            // 判断诊疗项目是否存在
            Medical_services existMedical_services = medical_servicesService.getById(id);
            if (existMedical_services == null) {
                return ResultVo.fail("诊疗项目信息不存在");
            }
            
            // 简单的删除操作，直接使用MyBatis-Plus
            boolean result = medical_servicesService.removeById(id);
            if (result) {
                return ResultVo.ok(true, "删除诊疗项目信息成功");
            } else {
                return ResultVo.fail("删除诊疗项目信息失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }
    
    @ApiOperation("批量删除诊疗项目信息")
    @PostMapping("/batch/delete")
    public ResultVo<Boolean> batchDelete(@RequestBody List<Long> ids) {
        try {
            if (ids == null || ids.isEmpty()) {
                return ResultVo.fail("请选择要删除的诊疗项目");
            }
            
            // 简单的批量删除操作，直接使用MyBatis-Plus
            boolean result = medical_servicesService.removeByIds(ids);
            if (result) {
                return ResultVo.ok(true, "批量删除诊疗项目信息成功");
            } else {
                return ResultVo.fail("批量删除诊疗项目信息失败");
            }
        } catch (Exception e) {
            return ResultVo.error(e);
        }
    }
}

